package be.groept.ace.enquete.model.beans;

import java.io.Serializable;
import java.util.List;
import java.util.SortedSet;
import java.util.StringTokenizer;
import java.util.TreeSet;

import be.groept.ace.enquete.model.interfaces.PersistentieValideerbaar;
import be.groept.ace.enquete.utils.ServletUtils;


public class Question implements Comparable<Question>, Serializable, PersistentieValideerbaar {
	private static final long serialVersionUID = 1L;
	private int id;
	private String text;
	private String possibleAnswer;
	private boolean freeText;
	private Survey survey;
	
	private SortedSet<Answer> answerSet;
	
	public Question() {
		answerSet = new TreeSet<Answer>();
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getPossibleAnswer() {
		return possibleAnswer;
	}

	public void setPossibleAnswer(String possibleAnswer) {
		this.possibleAnswer = possibleAnswer;
	}

	public Survey getSurvey() {
		return survey;
	}

	public void setSurvey(Survey survey) throws IllegalArgumentException {
		if(this.survey != null) {
			throw new IllegalArgumentException("Question cannot be updated, survey is already set");
		}
		this.survey = survey;
		//survey.addQuestion(this);
	}
	
	public void addAnswer(Answer answer) {
		answerSet.add(answer);
	}
	
	public void removeAnswer(Answer answer) {
		answerSet.remove(answer);
	}
	
	public SortedSet<Answer> getAnswerSet() {
		return answerSet;
	}
	
	public boolean containsAnswer(Answer answer) {
		return answerSet.contains(answer);
	}
	
	

	public boolean isFreeText() {
		return freeText;
	}

	public void setFreeText(boolean freeText) {
		this.freeText = freeText;
	}

	//compare by text
	@Override
	public int compareTo(Question o) {
		return  getText().compareTo(o.getText());
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj == null) {
			return false;
		}
		if( getClass() != obj.getClass()) {
			return false;
		}
		Question other = (Question)obj;
		
		return getText().equals(other.getText());
	}
	
	@Override
	public int hashCode() {
		return getText().hashCode();
	}
	
	@Override
	public String toString() {
		return String.format("Question object - id: %1$d - text: %2$s", getId(), getText());
	}

	@Override
	public boolean isValid(List<String> errors) {
		boolean valid = true;
		if(ServletUtils.isNullOrEmpty(getText())) {
			valid = false;
			errors.add("De vraag kan niet leeg zijn");
		}
		if(!isFreeText() && ServletUtils.isNullOrEmpty(getPossibleAnswer())) {
			valid = false;
			errors.add("Mogelijk antwoord mag niet leeg zijn indien het geen freetekst is");
		}
		StringTokenizer token = new StringTokenizer(getPossibleAnswer(), ",");
		if(!isFreeText() && token.countTokens() < 2) {
			valid = false;
			errors.add("Mogelijk antwoord is een lijst van antwoorden, gescheiden door een comma.");
		}
		if(survey == null) {
			valid = false;
			errors.add("Het was onmogelijk om de enquete toe te voegen");
		}
		return valid;
	}
	
}
